package com.controller;

import com.domain.ArticleResponseData;
import com.domain.BlogArticle;
import com.github.pagehelper.PageInfo;
import com.mapper.BlogArticleMapper;
import com.mapper.StatisticMapper;
import com.service.ArticleService;
import com.service.SiteService;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class ArticleController {
    @Autowired
    BlogArticleMapper blogArticleMapper;
    @Autowired
    StatisticMapper statisticMapper;
    @Autowired
    ArticleService articleService;
    @Autowired
    SiteService siteService;
    private static final Logger logger = LoggerFactory.getLogger(ArticleController.class);

    @GetMapping(value = "/admin/article/toEditPage")
    public String newArticle(){
        return "back/article_edit";
    }

    @PostMapping(value = "/admin/article/publish")
    @ResponseBody
    public ArticleResponseData publishArticle(BlogArticle article){
        if (StringUtils.isBlank(article.getCategories())){
            article.setCategories("默认分类");
        }
        try {
            articleService.publish(article);
            logger.info("文章发布成功");
            return ArticleResponseData.ok();
        }catch (Exception e){
            logger.error("文章发布失败，错误信息:"+e.getMessage());
            return ArticleResponseData.fail();
        }
    }

    @GetMapping(value = "/admin/article")
    public String index(@RequestParam(value = "page",defaultValue = "1") int page,
                        @RequestParam(value = "count",defaultValue = "10") int count,
                                    HttpServletRequest request){
        PageInfo<BlogArticle> pageInfo = articleService.selectArticleWithPage(page,count);
        request.setAttribute("articles",pageInfo);
        return "back/article_list";
    }

    @GetMapping(value = "/admin/article/{id}")
    public String esitArticle(@PathVariable("id") String id,HttpServletRequest request){
        BlogArticle article = blogArticleMapper.selectArticleWithId(Integer.parseInt(id));
        request.setAttribute("contents",article);
        request.setAttribute("categories",article.getCategories());
        return "back/article_edit";
    }

    @PostMapping(value = "/admin/article/modify")
    @ResponseBody
    public ArticleResponseData modifyArticle(BlogArticle article){
        try {
            blogArticleMapper.updateArticleWithId(article);
            logger.info("文章更新成功");
            return ArticleResponseData.ok();
        }catch (Exception e){
            logger.error("文章更新失败，错误信息:"+e.getMessage());
            return ArticleResponseData.fail();
        }
    }
    @PostMapping(value = "/admin/article/delete")
    @ResponseBody
    public ArticleResponseData delete(@RequestParam int id){
        try {
            blogArticleMapper.deleteArticleWithId(id);
            logger.info("文章删除成功");
            return ArticleResponseData.ok();
        }catch (Exception e){
            logger.error("文章删除失败，错误信息:"+e.getMessage());
            return ArticleResponseData.fail();
        }
    }
}
